var tempArr = [6,10,3,4,29,5,23,9,2]

function insertionSort(arr) {
    var len = arr.length;
    var preIndex, current;
    for (var i = 1; i < len; i++) {
        preIndex = i - 1;
        current = arr[i];
        while(preIndex >= 0 && arr[preIndex] > current) {
            arr[preIndex+1] = arr[preIndex];
            preIndex--;
        }
        arr[preIndex+1] = current;
    }
    return arr;
}
var bubbleSort = function ( array ) {
    var len = array.length;
    for (let i = 0; i < len; i++) {
        for (let j = 0; j < len-i-1; j++) {
            if(array[j] > array[j+1]){
                [array[j],array[j+1]] = [array[j+1],array[j]]           
            }
        }
    }
    // return array
}

var sectionSort = function ( array) {
    var len = array.length;
    for( let i = 0; i < len; i++){
        min = i;
        for (let j = i+1; j < len; j++) {
            if(array[j] < array[min]){
                min = j;
            }
        }
        [array[i], array[min]] = [array[min], array[i]]
    }
    // return array
}

/* var insertionSort = function (array) {
    var len = array.length;
    for(let i = 1; i< len; i++){
        for(let j = 0; j < i; j++){
            if(array[i]<array[j]){
                array.splice(j,0,array[i]);
                array.splice(i+1,1)
                break
            }
        }
    }
} */

/* var insertionSort = function (array) {
    var len = array.length;
    for (let i = 1; i < len; i++) {
        var temp = array[i];
        for( var j = i; j>0&&array[j-1]>temp; j--){
            array[j] = array[j-1]
        }
        array[j] = temp
    }
} */

// var bubbleResult = bubbleSort(tempArr)
// console.log(bubbleResult);

// var sectionResult = sectionSort(tempArr)
// console.log(tempArr);

insertionSort(tempArr);
console.log(tempArr)


